<!-- src/page/hrpages/hr_login.vue -->
<template>
  <div class="login-container" style="max-width: 400px; margin: 100px auto; padding: 30px; border: 1px solid #eaeaea; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1);">
    <h2 style="text-align: center; margin-bottom: 20px;">HR 登录</h2>
      <div style="text-align: center;">
        <el-link type="primary" @click="$router.push('/user_login')">用户登入</el-link>
      </div>
    <el-form :model="form" :rules="rules" ref="loginForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password" placeholder="请输入密码" @keyup.enter.native="submitLogin"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitLogin" style="width: 100%;">登录</el-button>
      </el-form-item>
      <div style="text-align: center;">
        <el-link type="primary" @click="$router.push('/hr_register')">还没有账号？去注册</el-link>
      </div>
    </el-form>
  </div>
</template>

<script>
import request from '@/utils/request';

export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    };
  },
  methods: {
    submitLogin() {
      this.$refs.loginForm.validate(valid => {
        if (!valid) return;

        request.post('/HR_login', this.form)
          .then(res => {
            if (res.data.code === 1) {
              const userData = res.data.data;
              // 存储 token 和用户信息
             
              localStorage.setItem('hr_token', userData.token);
              localStorage.setItem('hr_user', JSON.stringify(userData));
              
              console.log(userData);
              this.$message.success('登录成功');
              this.$router.push('/hr_main_page');
            } else {
              this.$message.error(res.data.msg || '用户名或密码错误');
            }
          })
          .catch(() => {
            this.$message.error('请求失败，请检查网络');
          });
      });
    }
  }
};
</script>

<style scoped>
.login-container {
  background: #fff;
}
</style>